package com.wyy.testtranaction.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyy.testtranaction.mapper.UserMapper;
import com.wyy.testtranaction.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.lang.model.SourceVersion;
import java.util.UUID;

/**
 * @Date: 2021/9/2
 * @Author: wyy
 */
@Service
public class UserService1 {
    @Autowired
    private UserMapper userMapper;


    public void insertNoTransaction() throws Exception {
        User user = new User();
        user.setId(System.currentTimeMillis());
        user.setName(UUID.randomUUID().toString());
        userMapper.insert(user);
        boolean flag = true;
        if (flag) {
            throw new Exception();
        }
    }

    @Transactional
    public void insertInTransaction() throws Exception {
        User user = new User();
        user.setId(System.currentTimeMillis());
        user.setName(UUID.randomUUID().toString());
        userMapper.insert(user);
        boolean flag = true;
        if (flag) {
            throw new Exception();
        }
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public void testRollback() throws Exception {
        userMapper.selectCount(new QueryWrapper<User>().lambda().ne(User::getId, "123"));
        User user = new User();
        user.setId(System.currentTimeMillis());
        user.setName(UUID.randomUUID().toString());
        System.out.println("user = " + user);
        userMapper.insert(user);

        boolean flag = true;
        if (flag) {
            int[] ints = new int[1];
            System.out.println(ints[10]);
        }
        user = new User();
        user.setId(System.currentTimeMillis());
        user.setName(UUID.randomUUID().toString());
        System.out.println("user = " + user);
        userMapper.insert(user);
    }
}
